package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.BaseEmployeeMapper;
import com.kaili.basic.mapper.BaseSupplyMapper;
import com.kaili.basic.pojo.entity.BaseSupply;
import com.kaili.basic.pojo.entity.BaseSupplyExport;
import com.kaili.basic.service.BaseSupplyService;
import com.kaili.common.entity.BaseEmployee;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.PinYinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by renzhikun on 18-6-11.
 */

@Service
@Transactional
public class BaseSupplyServiceImpl implements BaseSupplyService {
    @Autowired
    private BaseSupplyMapper baseSupplyMapper;
    @Autowired
    private BaseEmployeeMapper employeeMapper;

    @Override
    public void addSupply(BaseSupply baseSupply) {
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        if (uid != null) {
            baseSupply.setCreateBy(uid.longValue());
            baseSupply.setModifyBy(uid.longValue());
            baseSupply.setRegisterBy(uid.longValue());
            BaseEmployee employee = employeeMapper.selectById(uid);
            baseSupply.setCreateSite(employee.getSiteId().toString());//设置创建网点
            baseSupply.setPinyin(PinYinUtil.toSimplePinYin(baseSupply.getSupplyName()));
            baseSupply.setInitial(PinYinUtil.toSimplePinYin(baseSupply.getContacts()));
            baseSupplyMapper.addSupply(baseSupply);
        }
    }

    @Override
    public void deleteSupply(Long id) {
        Map<String, Object> map = new HashMap();
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        if (uid != null) {
            map.put("modifyBy", uid.longValue());
            map.put("supplyId",id);
            baseSupplyMapper.deleteSupply(map);
        }
    }

    @Override
    public void updateSupply(BaseSupply baseSupply) {
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        if (uid != null) {
            baseSupply.setModifyBy(uid.longValue());
            baseSupply.setPinyin(PinYinUtil.toSimplePinYin(baseSupply.getSupplyName()));
            baseSupply.setInitial(PinYinUtil.toSimplePinYin(baseSupply.getContacts()));
            baseSupplyMapper.updateSupply(baseSupply);
        }
    }

    @Override
    public Page<BaseSupply> querySupply(Page<BaseSupply> page, BaseSupply baseSupply) {
        List<BaseSupply> baseSupplies = baseSupplyMapper.querySupply(page, baseSupply);
        return page.setRecords(baseSupplies);
    }

    @Override
    public List<String> getSupplyCodeForList() {
        return baseSupplyMapper.getSupplyCodeForList();
    }

    @Override
    public List<BaseSupply> getSupplyByNameAndContacts(BaseSupply baseSupply) {
        return baseSupplyMapper.getSupplyByNameAndContacts(baseSupply);
    }

    @Override
    public String getMaxSupplyCode() {
        return baseSupplyMapper.getMaxSupplyCode();
    }

    @Override
    public List<BaseSupplyExport> querySupplyExport(Map<String, Object> map) {
        return baseSupplyMapper.querySupplyExport(map);
    }

    @Override
    public List<BaseSupply> getSupplyLikeName(String supplyName) {
        return baseSupplyMapper.getSupplyLikeName(supplyName);
    }
}
